package blip.jcaf.cm;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import blip.jcaf.cm.monitor.Arrival;

public class EntryMonitor implements Runnable {
	public static void main(String[] args) { // port arg
		
	}

	@Override
	public void run() {
		try {
			ServerSocket ss = new ServerSocket(12345);
			while (true) {
				Socket con = ss.accept(); // blocking io!
				InputStreamReader in = new InputStreamReader(
						con.getInputStream());
				StringBuffer btId = new StringBuffer();
				int c;
				while ((c = in.read()) != 0)
					// 0 as end mark
					btId.append((char) c);
				PrintWriter out = new PrintWriter(con.getOutputStream());

				System.out.println("DEVICE: " + btId + " LOCATED AT ITU");
				
				out.flush();
				con.close();
				new Arrival(btId.toString()).visitorArrived(btId.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}